package com.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.IdWorker;
import com.changgou.goods.dao.*;
import com.changgou.goods.pojo.*;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/****
 * @Author:传智播客
 * @Description:Category业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private TemplateMapper templateMapper;

    @Autowired(required = false)
    private ParaMapper paraMapper;

    @Autowired(required = false)
    private SpuMapper spuMapper;

    @Autowired(required = false)
    private IdWorker idWorker;

    @Autowired(required = false)
    private BrandMapper brandMapper;

    @Autowired(required = false)
    private SkuMapper skuMapper;


    /**
     * Category条件+分页查询
     *
     * @param category 查询条件
     * @param page     页码
     * @param size     页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Category> findPage(Category category, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(category);
        //执行搜索
        return new PageInfo<Category>(categoryMapper.selectByExample(example));
    }

    /**
     * Category分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Category> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Category>(categoryMapper.selectAll());
    }

    /**
     * Category条件查询
     *
     * @param category
     * @return
     */
    @Override
    public List<Category> findList(Category category) {
        //构建查询条件
        Example example = createExample(category);
        //根据构建的条件查询数据
        return categoryMapper.selectByExample(example);
    }


    /**
     * Category构建查询对象
     *
     * @param category
     * @return
     */
    public Example createExample(Category category) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        if (category != null) {
            // 分类ID
            if (!StringUtils.isEmpty(category.getId())) {
                criteria.andEqualTo("id", category.getId());
            }
            // 分类名称
            if (!StringUtils.isEmpty(category.getName())) {
                criteria.andLike("name", "%" + category.getName() + "%");
            }
            // 商品数量
            if (!StringUtils.isEmpty(category.getGoodsNum())) {
                criteria.andEqualTo("goodsNum", category.getGoodsNum());
            }
            // 是否显示
            if (!StringUtils.isEmpty(category.getIsShow())) {
                criteria.andEqualTo("isShow", category.getIsShow());
            }
            // 是否导航
            if (!StringUtils.isEmpty(category.getIsMenu())) {
                criteria.andEqualTo("isMenu", category.getIsMenu());
            }
            // 排序
            if (!StringUtils.isEmpty(category.getSeq())) {
                criteria.andEqualTo("seq", category.getSeq());
            }
            // 上级ID
            if (!StringUtils.isEmpty(category.getParentId())) {
                criteria.andEqualTo("parentId", category.getParentId());
            }
            // 模板ID
            if (!StringUtils.isEmpty(category.getTemplateId())) {
                criteria.andEqualTo("templateId", category.getTemplateId());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Category
     *
     * @param category
     */
    @Override
    public void update(Category category) {
        categoryMapper.updateByPrimaryKey(category);
    }

    /**
     * 增加Category
     *
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    /**
     * 根据ID查询Category
     *
     * @param id
     * @return
     */
    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Category全部数据
     *
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.selectAll();
    }

    /**
     * 查询分类分类
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Category> findCateGoryParentId(Integer parentId) {
        List<Category> list = categoryMapper.findCateGoryParentId(parentId);
        return list;
    }

    /**
     * 根据信息分类查询  查询商品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> findCategoryBrandById(Integer categoryId) {
        List<Brand> list = categoryMapper.findCategoryBrandById(categoryId);
        return list;
    }

    /**
     * 查询模板
     *
     * @param id
     * @return
     */
    @Override
    public Template findCategoryTemplate(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return templateMapper.selectByPrimaryKey(category.getTemplateId());
    }

    @Autowired(required = false)
    private SpecMapper specMapper;

    /**
     * @return
     * @Description 查询规格
     * @ClassName CategoryController
     * @date 2019/11/13 19:23
     */
    @Override
    public List<Spec> findByCategoryId(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        Spec spec = new Spec();
        spec.setSeq(category.getSeq());
        List<Spec> list = specMapper.select(spec);
        return list;
    }


    /**
     * @return
     * @Description 根据categoryId查询Para
     * @ClassName CategoryController
     * @date 2019/11/13 19:35
     */
    @Override
    public Para findCategoryByPara(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        Para para = new Para();
        para.setSeq(category.getSeq());
        List<Para> paras = paraMapper.select(para);
        return paras.get(0);
    }


    /**
     * @return
     * @Description 保存Goods
     * @ClassName CategoryController
     * @date 2019/11/13 18:27
     */
    @Override
    public void saveUpdate(Goods goods) {
        //保存基本的信息spu
        Spu spu = goods.getSpu();
        if (spu.getId() == null) {
            spu.setId(idWorker.nextId());
            spu.setIsDelete("0");
            spu.setIsMarketable("0");
            spu.setStatus("0");
            spuMapper.insertSelective(spu);
        } else {
            spu.setStatus("0");
            spuMapper.updateByPrimaryKeySelective(goods.getSpu());
            Sku sku = new Sku();
            sku.setSpuId(spu.getId());
            skuMapper.delete(sku);
        }

            //保存详细信息（sku）
            List<Sku> skuList = goods.getSkuList();
            if (skuList != null && skuList.size() > 0) {
                for (Sku sku : skuList) {

                    sku.setId(idWorker.nextId());
                    String name = spu.getName() + " " + spu.getCaption();
                    String spec = sku.getSpec();
                    Map<String, String> specMap = JSON.parseObject(spec, Map.class);
                    if (specMap != null) {
                        Set<Map.Entry<String, String>> entries = specMap.entrySet();
                        for (Map.Entry<String, String> entry : entries) {
                            name += " " + entry.getValue();
                        }
                    }
                    sku.setName(name);
                    sku.setUpdateTime(new Date());
                    sku.setCategoryId(spu.getCategory3Id());
                    sku.setSpuId(spu.getId());
                    sku.setCategoryName(categoryMapper.selectByPrimaryKey(spu.getCategory3Id()).getName());
                    sku.setBrandName(brandMapper.selectByPrimaryKey(spu.getBrandId()).getName());
                    sku.setStatus("1");
                    skuMapper.insertSelective(sku);
                }
            }else{
                for (Sku sku : skuList) {
                    skuMapper.updateByPrimaryKeySelective(sku);
                }
            }


    }

}
